﻿Public Class FMSiswa

    'MEMBER VARIABLES
    Private _cSiswa As CSiswa
    Private _dView As DataView

    'forms
    Private WithEvents _fDetilSiswa As FTPendaftaran
    Private _fdSiswa As FDSiswa

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.Text = My.Settings.NamaAplikasi & " [Master Siswa]"
    End Sub

#Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles LabelJudul.MouseDown, PanelJudul.MouseDown, MyBase.MouseDown

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp, LabelJudul.MouseUp, PanelJudul.MouseUp

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove, LabelJudul.MouseMove, PanelJudul.MouseMove

        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub


    Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PanelJudul.Paint
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(ByVal cont As Control, ByVal g As Graphics, ByVal p As Pen)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
            ElseIf TypeOf ctl Is Panel Then
                PaintTextBox(ctl, g, p)
            ElseIf TypeOf ctl Is GroupBox Then
                PaintTextBox(ctl, g, p)
            End If
        Next
    End Sub

    Private Sub SetRegionTextBox(ByVal cont As Control)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            End If
        Next
    End Sub

    Private Sub btnMaximize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximize.Click

        WindowState = FormWindowState.Maximized
        btnMinimize.Visible = False
        btnMaximize.Visible = False
        btnClose.Visible = False
    End Sub

    Private Sub btnMinimize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinimize.Click
        WindowState = FormWindowState.Minimized
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub FSizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        If WindowState <> FormWindowState.Maximized Then
            btnMinimize.Visible = True
            btnMaximize.Visible = True
            btnClose.Visible = True
        End If
    End Sub
    '-----------------------------------------------------------------------------------------------
#End Region

    Private Sub KeluarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KeluarToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub TampilkanData()
        Try
            _dView = _cSiswa.getDisplay.DefaultView
            DGMaster.DataSource = _dView

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub

    Private Sub FMSiswa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'set text box border
        SetRegionTextBox(Me)

        'tampilkan data ke control
        _cSiswa = New CSiswa
        TampilkanData()
    End Sub

    Private Sub DGMaster_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGMaster.RowPostPaint
        'menampilkan nomor urut pada datagridview
        DGMaster.Rows(e.RowIndex).Cells("CNo").Value = e.RowIndex + 1
        'set text pada tombol tampil
        DGMaster.Rows(e.RowIndex).Cells("CDetil").Value = "detil"
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        _fDetilSiswa = New FTPendaftaran(DGMaster.CurrentRow.Cells("CID").Value)
        _fDetilSiswa.showDetil()
        
    End Sub

    Private Sub _fDetilSiswa_DataTerupdate() Handles _fDetilSiswa.DataTerupdate
        'refresh data
        TampilkanData()
    End Sub

    Private Sub RubahToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RubahToolStripMenuItem.Click
        _fDetilSiswa = New FTPendaftaran(DGMaster.CurrentRow.Cells("CID").Value)
        _fDetilSiswa.showEdit()

    End Sub

    Private Sub HapusToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HapusToolStripMenuItem.Click
        'hapus data
        If MessageBox.Show("Hapus data ini?", "Peringatan", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            _cSiswa.hapus(DGMaster.CurrentRow.Cells("CID").Value)
            TampilkanData()
        End If
    End Sub

    Private Sub DGMaster_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGMaster.CellContentClick
        If TypeOf DGMaster.CurrentRow.Cells(e.ColumnIndex) Is DataGridViewButtonCell Then
            '_fdSiswa = New FDSiswa(DGMaster.CurrentRow.Cells("CID").Value)
            ''_fdSiswa.MdiParent = Me.MdiParent
            '_fdSiswa.Show()
            Button5.PerformClick()
        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        TambahToolStripMenuItem.PerformClick()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        RubahToolStripMenuItem.PerformClick()
    End Sub

    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
        'ToolStripMenuItem1.PerformClick()
        'form cover
        Dim fCov As New FCover
        fCov.Show()

        _fdSiswa = New FDSiswa(DGMaster.CurrentRow.Cells("CID").Value)
        _fdSiswa.ShowDialog()

        fCov.Close()
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        HapusToolStripMenuItem.PerformClick()
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        KeluarToolStripMenuItem.PerformClick()
    End Sub

    Private Sub TBNama_TextChanged(sender As System.Object, e As System.EventArgs) Handles TBNama.TextChanged
        'filter by nama
        _dView.RowFilter = cbFilterSd.Text & " like '%" & TBNama.Text.Trim & "%'"
    End Sub

    Private Sub cbFilter_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbFilter.SelectedIndexChanged
        cbFilterSd.SelectedIndex = cbFilter.SelectedIndex
    End Sub
End Class